package com.wonders.hisdoctor.station.project.record.controller;

import com.wonders.his.common.bean.BaseResult;
import com.wonders.hisdoctor.station.project.base.domain.WdeditorDs;
import com.wonders.hisdoctor.station.project.base.domain.WdeditorHistory;
import com.wonders.hisdoctor.station.project.base.domain.WdeditorPritemplatedetail;
import com.wonders.hisdoctor.station.project.base.domain.WdeditorPritemplatemaster;
import com.wonders.hisdoctor.station.project.record.service.IWdEditorService;
import com.wonders.hisdoctor.station.project.record.vo.*;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Description: 病例模板Controller
 *
 * @author tong-aj
 * <p>
 * Create: 2021-02-23 20:24
 */
@RestController
@RequestMapping("/WdEditor")
public class WdEditorController {

    private final IWdEditorService wdEditorService;

    public WdEditorController(IWdEditorService wdEditorService) {
        this.wdEditorService = wdEditorService;
    }

    // StepOne：选择表
    @ApiOperation("查询控件需要绑定的表")
    @GetMapping("/getWDEditorDSList")
    public BaseResult<WdeditorDs> getWDEditorDSList() {
        return BaseResult.success(wdEditorService.getWDEditorDSList());
    }

    // Step Two：选择表对应的字段
    @ApiOperation("根据表，选择相应的字段名")
    @PostMapping("/getTableColumns")
    public BaseResult<String> getTableColumns(@RequestBody WdeditorDs wdeditorDs) {
        return BaseResult.success(wdEditorService.getTableColumns(wdeditorDs));
    }

    // Step Three：查询表对应字段的默认值
    @ApiOperation("查询表对应字段的默认值")
    @GetMapping("getColumnValues/{tableName}/{codeColumn}/{valueColumn}")
    public BaseResult<String> getColumnValues(@PathVariable("tableName") String tableName, @PathVariable("codeColumn") String codeColumn, @PathVariable("valueColumn") String valueColumn) {
        return BaseResult.success(wdEditorService.getColumnValues(tableName,codeColumn,valueColumn));
    }

    // Step Four：保存接口列表
    @ApiOperation("保存接口列表")
    @GetMapping("/getSaveInterface")
    public BaseResult<String> getSaveInterface(){
        return BaseResult.success(wdEditorService.getSaveInterface());
    }

    // Step Five：保存接口参数列表
    @ApiOperation("保存接口参数列表")
    @GetMapping("/getInterfaceParams")
    public BaseResult<String> getInterfaceParams(@RequestParam("interfaceName") String interfaceName){
        return BaseResult.success(wdEditorService.getInterfaceParams(interfaceName));
    }

    // Step Six：保存病历模板
    @ApiOperation("保存病历模板")
    @PostMapping("/saveWdEditors")
    public BaseResult<String> saveWdEditors(@RequestBody List<WdEditorVo> list){
        if(CollectionUtils.isEmpty(list)){
            return BaseResult.success("无内容，无需保存");
        }
        wdEditorService.saveWdEditors(list);
        return BaseResult.success();
    }

    // Step Seven：根据模板Id获取模板信息
    @ApiModelProperty("测试模板查询优化")
    @GetMapping("/getWdEditors/{modelId}")
    public BaseResult<String> getWdEditors(@PathVariable("modelId") String modelId,
                                           String patientId, String visitNo,
                                           @DateTimeFormat(pattern = "yyyy/MM/dd") Date visitDate,
                                           String tId,String empNo,HttpServletRequest request){
        return BaseResult.success(wdEditorService.getWdEditorsByModelId(modelId,patientId,visitNo,visitDate,tId,empNo,request));
    }

    // Step Eight：保存模板html信息
    @ApiOperation("保存模板html信息")
    @PostMapping("/saveWdEditorsHtml")
    public BaseResult<String>saveWdEditorsHtml(@RequestBody WdEditorsHtmlVo wdEditorsHtmlVo){
        wdEditorService.saveWdEditorsHtml(wdEditorsHtmlVo);
        return BaseResult.success();
    }

    // Step Nine：读取模板Html信息
    @ApiOperation("读取模板Html信息")
    @GetMapping("/getWdEditorsHtml/{modelId}")
    public BaseResult<WdEditorsHtmlVo> getWdEditorsHtml(@PathVariable("modelId") String modelId){
        return BaseResult.success(wdEditorService.getWdEditorsHtml(modelId));
    }

    // Step Ten：提供模板列表
    @ApiOperation("提供模板列表")
    @GetMapping("/getModelList/{hospitalCode}")
    public BaseResult<Map<String,Object>> getModelList(@PathVariable("hospitalCode") String hospitalCode, String fastFind){
        return BaseResult.success(wdEditorService.getModelList(hospitalCode,fastFind));
    }

    @ApiOperation("批量删除模板")
    @PostMapping("deleteModels")
    public BaseResult<String> deleteModels(@RequestBody List<String> modelIds){
        wdEditorService.deleteModels(modelIds);
        return BaseResult.success();
    }

    @ApiOperation("保存控件历史记录")
    @PostMapping("saveHistories")
    public BaseResult<String> saveHistories(@RequestBody List<WdeditorHistory> list){
        wdEditorService.saveHistories(list);
        return BaseResult.success();
    }

    @ApiOperation("加载病历模板")
    @PostMapping("getTemplateDetails")
    public BaseResult<WdeditorPritemplatedetail> getTemplateDetails(@RequestBody WdeditorPritemplatemaster templateRequest){
        String operate = templateRequest.getOperate();
        String orgId = templateRequest.getOrgId();
        String templateName = templateRequest.getTemplateName();
        List<WdeditorPritemplatedetail> details = wdEditorService.getTemplateDetails(orgId,operate,templateName);
        return BaseResult.success(details);
    }

    @ApiOperation("通过modelId获取下拉控件的静态数据源需要信息")
    @GetMapping("/getStaticEditors/{modelId}")
    public BaseResult<List<StaticEditor>> getStaticEditors(@PathVariable("modelId")String modelId){
        return BaseResult.success(wdEditorService.getStaticEditors(modelId));
    }

    @ApiOperation("根据setId和dId获取该控件的静态数据字典")
    @GetMapping("/getStaticDataList/{setId}/{dId}")
    public BaseResult<Map<String, List<Map<String, Object>>>> getStaticDataList(@PathVariable("setId")String setId,@PathVariable("dId")String dId ){
        return BaseResult.success(wdEditorService.getStaticDataList(setId,dId));
    }

    @ApiOperation("设为默认加载模板")
    @GetMapping("/setDefaultModel/{orgId}/{modelId}")
    public BaseResult<String> setDefaultModel(@PathVariable("orgId") String orgId, @PathVariable("modelId") String modelId){
        wdEditorService.setDefaultModel(orgId,modelId);
        return BaseResult.success("设置成功");
    }

    @ApiOperation("加载默认模板")
    @GetMapping("/getDefaultModelId/{orgId}")
    public BaseResult<String> getDefaultModelId (@PathVariable ("orgId") String orgId){
        return BaseResult.success(wdEditorService.getDefaultModelId(orgId));
    }

    // ========================病历模板========================
    @ApiOperation("病历模板列表")
    @GetMapping("getTemplateList/{orgId}")
    public BaseResult<TemplateListVo> getTemplateList(@PathVariable("orgId") String orgId, String effectiveDept, String templateName, String condition){
        return BaseResult.success(wdEditorService.getTemplateList(orgId,effectiveDept,templateName,condition));
    }

    @ApiOperation("保存病历模板数据")
    @PostMapping("saveTemplates")
    public BaseResult<String> saveTemplates(@RequestBody TemplateVo templateVo){
        wdEditorService.saveTemplates(templateVo);
        return BaseResult.success();
    }

    @ApiOperation("批量删除病历模板")
    @PostMapping("deleteTemplateList")
    public BaseResult<String> deleteTemplateList(@RequestBody List<String> tIds){
        wdEditorService.deleteTemplateList(tIds);
        return BaseResult.success();
    }
}
